﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoGear.BLL;

public partial class purchase_gift_certificate : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ClearPage();
        }
    }

    public void ClearPage()
    {
        tbAmount.Text = "";
        tbFromEmail.Text = "";
        tbFromName.Text = "";
        tbMessege.Text = "";
        tbQuantity.Text = "";
        tbToEmail.Text = "";
        tbToName.Text = "";
    }

    protected void ButtonSubmit_Click(object sender, ImageClickEventArgs e)
    {
        //Page.Validate();
        //if (IsValid)
        {
            string messege = string.Empty;
            double amount = Convert.ToDouble(tbAmount.Text.Trim());
            int quantity = Convert.ToInt32(tbQuantity.Text.Trim());
            if (this.AddGiftCardToShoppingCart(
                tbFromEmail.Text.Trim(), tbFromName.Text.Trim(),
                tbToEmail.Text.Trim(), tbToName.Text.Trim(),
                tbMessege.Text.Trim(), amount, quantity, out messege))
            {
                ClearPage();

                //update the count of shopping bag
                Label lblCartItems = Page.Master.Master.FindControl("lblCartItems") as Label;
                //lblCartItems.Text = AutoGearSessionManager.ShoppingCart.CartItems.Count + AutoGearSessionManager.ShoppingCart.GiftItems.Count + " Items";
                lblCartItems.Text = AutoGearSessionManager.ShoppingCart.TotalCartItem + AutoGearSessionManager.ShoppingCart.TotalGiftItem + " Items";

                //rpGiftCertificate.Message = messege;
                rpGiftCertificate.SetSuccessMessage(messege);
            }
            else
            {
                //rpGiftCertificate.Message = messege;
                //rpGiftCertificate.MessageType = SiteGlobals.ResponseMessageType.Error;
                rpGiftCertificate.SetFailMessage(messege);
            }
        }
    }

    private bool AddGiftCardToShoppingCart(string fromEmail, string fromName,
        string toEmail, string toName, string message, double amount, int quantity,
        out string msg)
    {
        msg = string.Empty;
        //int shoppingCartID = GetShoppingCartID();
        int giftCardExpiration = 3;//months
        string giftCertificateCode = Guid.NewGuid().ToString(); //GiftCertificateManager.GetGiftCertificateCode();
        //int cartGiftCertificateID = ShoppingCartGrftCertificate.CreateNewShoppingCartGrftCertificate(shoppingCartID, giftCertificateCode, fromEmail, fromName, toEmail, toName, message, amount, quantity, amount, DateTime.Now, DateTime.Now.AddMonths(giftCardExpiration));

        GiftItem giftItem = new GiftItem(Guid.NewGuid().ToString(), giftCertificateCode, fromEmail, fromName,
            toEmail, toName, message, quantity, amount, DateTime.Now, DateTime.Now.AddMonths(giftCardExpiration));

        ShoppingCart tempShoppingCart = AutoGearSessionManager.ShoppingCart;
        tempShoppingCart.AddGiftItem(giftItem);
        AutoGearSessionManager.ShoppingCart = tempShoppingCart;

        //if (cartGiftCertificateID > 0)
        //{
        //    ShoppingCartOrder shoppingCart = ShoppingCartOrder.GetShoppingCartOrderByShoppingCartOrderId(shoppingCartID);
        //    if (shoppingCart != null)
        //    {
        //        shoppingCart.SubTotalAmount += (decimal)amount;
        //    }
        msg = "your gift certificate amounting " + string.Format("{0:c}", amount) + " added to shopping cart successfully.";
        return true;
        //}
        //else
        //{
        //    msg = "failed to add gift certificate to your cart, please try again.";
        //    return false;
        //}
    }
}
